egglog rules to latex repr#21
Conversation
…d rendering s-expressions to latex
…rom egglog_to_inference for latex rendering, conditional display for jupyter nb
|
updated. latex rendering util can be used by importing it from - and placing it on scripts with notebook check - |
There was a problem hiding this comment.
Pull Request Overview
This PR adds functionality to convert Egglog rules and rewrites into LaTeX representations and integrates notebook visualization for those rulesets.
- Introduces
egglog_to_latex.pywith S-expression parsing, LaTeX conversion, and notebook display. - Hooks into chapter 3 tutorial script to visualize constant-propagation and if-else folding rulesets when in a notebook.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| sealir-tutorials/egglog_to_latex.py | New module for tokenizing S-expressions, converting them to LaTeX, and visualizing in notebooks |
| sealir-tutorials/ch03_egraph_program_rewrites.py | Added imports and if IN_NOTEBOOK blocks to display LaTeX-rendered rulesets |
Comments suppressed due to low confidence (2)
sealir-tutorials/egglog_to_latex.py:89
- Consider adding a docstring to
_sexp_texexplaining its purpose and how it handles both str and list inputs.
def _sexp_tex(x) -> str:
sealir-tutorials/egglog_to_latex.py:5
- Add unit tests for
tokenize(andparse_sexps) to ensure S-expression edge cases are correctly handled.
def tokenize(egglog_str: str) -> List[str]:
| tokens = tokenize(egglog_str) | ||
| sexps = parse_sexps(tokens) | ||
|
|
||
| from IPython.display import display, Math |
There was a problem hiding this comment.
[nitpick] Move the IPython.display import to the top of the module to avoid runtime overhead when calling visualize_ruleset_latex multiple times.
| from sealir import rvsdg | ||
| from sealir.eqsat import rvsdg_eqsat | ||
| from sealir.eqsat.rvsdg_eqsat import GraphRoot, Term, TermList | ||
| from utils import IN_NOTEBOOK |
There was a problem hiding this comment.
If this script is intended to be run as a module, consider using a relative import (from .utils import IN_NOTEBOOK) to avoid ImportError when the package is installed.
| from utils import IN_NOTEBOOK | |
| from .utils import IN_NOTEBOOK |
| if IN_NOTEBOOK: | ||
| # Visualize the constant propagation ruleset | ||
| visualize_ruleset_latex(ruleset_const_propagate) | ||
|
|
There was a problem hiding this comment.
[nitpick] These if IN_NOTEBOOK blocks are repeated—consider extracting to a helper function to DRY up the visualization calls.
| if IN_NOTEBOOK: | |
| # Visualize the constant propagation ruleset | |
| visualize_ruleset_latex(ruleset_const_propagate) | |
| def visualize_if_in_notebook(ruleset): | |
| if IN_NOTEBOOK: | |
| # Visualize the given ruleset | |
| visualize_ruleset_latex(ruleset) | |
| visualize_if_in_notebook(ruleset_const_propagate) |
|
Long rewrite rules can be too long for the screen; e.g. the pade44 tanh expansion in demo1 The denominator gets very long. Needs a way to split the long equations. |
|
I used a modified version of this with ability to replace long names for shorter ones. Compare with https://github.com/numba/numba-prototypes/blob/archive/scipy_2025_slides/sealir-tutorials/utils/egglog_to_latex.py |


for better rulesets readability,
use egraph.as_egglog_string to get all S-expressions,
then convert each rule(...) and rewrite(...) form into its LaTeX representation.
rendered rules and rewrite:
